package com.tongji.showroom.model
{
	import com.adobe.serialization.json.JSON;
	import com.tongji.showroom.view.twoD.Furniture;
	import com.tongji.showroom.vo.Model;
	import com.tongji.showroom.vo.UserAngle;
	import com.tongji.showroom.vo.UserScene;
	
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author frank
	 * 2011/09/02
	 * 实现对场景的保存
	 */
	[Bindable]
	public class UserSceneModel extends Actor
	{
		public var enable:Boolean = false;
		public var saveSceneArray:ArrayCollection;//该变量存储了多个场景内容
		public var currentScene:Object;
		public var userProduct:Object;
		
		public var scene:String;
		public var sceneArr:ArrayCollection;
		public var getUserSceneArr:ArrayCollection;
		
		//caddy
		public var currentPosition:Point;
		public var currentFurniture:Furniture;
		public var userAngles:Array = new Array();
		public var userSceneArray:ArrayCollection = new ArrayCollection();
		
		public var images:ArrayCollection;
		
		public var currentUserScene:Object = new Object();
		
		public var recordCount:Number;
		public function UserSceneModel()
		{
			super();
		}
		
		public function updateUserScene(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			getUserSceneArr = new ArrayCollection(sceneArr[0].VR_SceneList);
			recordCount = sceneArr[0].PageCount;
		}
		/**
		 * @author caddy 
		 * @param images
		 * 
		 */		
		public function updateImage(images:ArrayCollection):void
		{
			this.images = images;
		}
		/**保存用户自定义的场景
		 * @author caddy 
		 * @param currentScene
		 * @param userProduct
		 * 
		 */		
		public function saveScene(currentScene:Object, userProduct:Object = null):void
		{
			this.currentScene = currentScene;
			var userScene:Object = new Object();
			userScene.ImgUrl = "";
			userScene.state = 1;
			userScene.uId = "22";
			var SceneAngleList:Object = [];
			var angleCount:int = currentScene.SceneAngleList.length;
			var angle:Object;
			var info:Object;
			for (var i:int = 0; i < angleCount; i++)
			{
				angle = new Object();
				var currentAngle:Object = currentScene.SceneAngleList[i];
				angle.Asid = currentAngle.Asid;
				var infoCount:int = currentAngle.SceneInfoList.length;
				var SceneInfoList:Object = [];
				for (var k:int = 0; k < infoCount; k++)
				{
					info = new Object();
					var currentInfo:Object = currentAngle.SceneInfoList[k];
					info.siId = currentInfo.siId;
					info.State = currentInfo.State;
					SceneInfoList[k] = info;
				}
				angle.SceneInfoList = SceneInfoList;
				SceneAngleList[i] = angle;
			}
			userScene.SceneAngleList = SceneAngleList;
			
			var userScentString:String = JSON.encode(userScene);
		
		}
		
		public  function save(currentScene:Object, userID:Number):String
		{
			//var scene:UserScene = new UserScene();
			var userScene:Object = new Object();
			userScene.ImgUrl = images[0];
			userScene.state = 1;
			userScene.uId = userID;
			userScene.sid = currentScene.sid;
			var SceneAngleList:Object = [];
			var angleCount:int = currentScene.SceneAngleList.length;
			var angle:Object;
			var info:Object;
			for(var i:int = 0; i < userAngles.length; i++)
			{
				angle = new Object();
				angle.Asid = userAngles[i].sceneAngleID;
				angle.Jdid = userAngles[i].angleID;
				/*angle.Bgurl = scene.img;*/
				angle.Bgurl = images[i];
				SceneAngleList[i] = angle;
			}
			userScene.SceneAngleList = SceneAngleList;
			var userScentString:String = JSON.encode(userScene);
			return userScentString;
		}
	}
}